package com.tomorrow.runner.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.tomorrow.runner.R;
import com.tomorrow.runner.activity.LoginActivity;
import com.tomorrow.runner.activity.basic.BasicActivity;
import com.tomorrow.runner.data.bean.UserBean;
import com.tomorrow.runner.service.Result;
import com.tomorrow.runner.util.Define;
import com.tomorrow.runner.widget.UserForm;

import cn.sucun.android.utils.DateUtils;

public class UserInfoActivity extends BasicActivity implements View.OnClickListener, Validator.ValidationListener {

    private UserForm mUserForm;
    private UserBean mUserBean;
    private Button mSubmit;
    private Button mLogout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        mSubmit = (Button) findViewById(R.id.btn_submit);
        mLogout = (Button) findViewById(R.id.btn_logout);
        mSubmit.setOnClickListener(this);
        mLogout.setOnClickListener(this);
        mUserForm = new UserForm(this,UserForm.MODEL_EDIT);
        mUserForm.setValidationListener(this);
        initUserInfo();
    }

    private void initUserInfo() {
        mUserBean = mAccountService.getUserInfo();
        mUserForm.mEditUsername.setText(mUserBean.username);
        mUserForm.mEditNickname.setText(mUserBean.nickname);
        mUserForm.mRadioMale.setChecked(mUserBean.gender);
        mUserForm.mRadioFeMale.setChecked(!mUserBean.gender);
        mUserForm.mBirthday.setText(DateUtils.formatDateString(mUserBean.birthday,false));
        mUserForm.mEditWeight.setText(String.valueOf(mUserBean.weight));
        mUserForm.mEditTall.setText(String.valueOf(mUserBean.tall));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                mUserForm.validate();
                break;
            case R.id.btn_logout:
                mAccountService.logout(mUserBean.username);
                Intent intent = new Intent(Define.ACTION_LOGOUT);
                sendBroadcast(intent);
                goToLogin();
                break;
        }
    }

    private void goToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public void onValidationSucceeded() {
        Result result = mAccountService.updateUser(mUserBean);
        Throwable error = result.getError();
        if (error == null){
            showMsgToast("更新成功");
        }else{
            showMsgToast(error.getMessage());
        }

    }

    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {
        if (failedView instanceof TextView) {
            ((TextView) failedView).setError(failedRule.getFailureMessage());
        } else {
            showMsgToast(failedRule.getFailureMessage());
        }
    }
}
